package com.vcredit.bird.service.base;

import java.net.SocketTimeoutException;

import com.vcredit.bird.service.foreignTradeCompliance.ForeignTradeComplianceService;
import org.apache.commons.lang.StringUtils;

import com.fasterxml.jackson.core.type.TypeReference;
import com.vcredit.creditcat.exception.BirdRequestException;
import com.vcredit.creditcat.exception.BirdRequestFailException;
import com.vcredit.framework.util.Base64Util;
import com.vcredit.framework.util.HttpRequest;
import com.vcredit.framework.util.JacksonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BirdService {
	private Logger logger = LoggerFactory.getLogger(ForeignTradeComplianceService.class);
	private static int ERRORCODE = 1;
	private static int RUNTIMEERRORCODE = 110;
	private static int THIRDREQUESTERRORCODE = 500;

	/**
	 * 默认超时时间,单位为秒
	 */
	private static int DEFAULT_TIMEOUT = 30;

	protected BirdResponse getBirdData(String url) throws BirdRequestException {
		return getBirdData(url, new int[] { 0 });
	}

	protected BirdResponse getBirdData(String url, int[] successCode) throws BirdRequestException {
		try {
			String result = HttpRequest.doGet(url);
			BirdResponse response = operate(result, successCode);
			response.setOriginalResult(result);
			return response;
		} catch (Exception ex) {
			logger.error("getBirdData"+ex.toString());
			throw new BirdRequestException(ex.getMessage());
		}
	}

	protected BirdResponse postBirdData(String url, String postData) throws Exception {
		return postBirdData(url, postData, new int[] { 0 }, DEFAULT_TIMEOUT);
	}

	protected BirdResponse postBirdData(String url, String postData, Integer timeout) throws Exception {
		return postBirdData(url, postData, new int[] { 0 }, timeout);
	}

	protected BirdResponse postBirdData(String url, String postData, int[] successCode, Integer timeout)
			throws Exception {
		if (StringUtils.isBlank(postData)) {
			return null;
		}
		try {
			String result = HttpRequest.doJson(url, Base64Util.base64Encode(postData), timeout);
			BirdResponse response = operate(result, successCode);
			response.setOriginalResult(result);
			return response;
		} catch (Exception ex) {
			if (ex instanceof SocketTimeoutException) {
				throw new SocketTimeoutException(ex.getMessage());
			} else {
				throw new BirdRequestException(ex.getMessage());
			}

		}
	}

	private BirdResponse operate(String result, int[] successCode) throws Exception {
		BirdResponse response = JacksonUtil.Json2Obj(result, new TypeReference<BirdResponse>() {
		});
		if(null != response && StringUtils.isNotBlank(response.getDesc())) {
			response.setDesc(response.getDesc().replaceAll("\"", ""));
		}
		for (int i = 0; i < successCode.length; i++) {
			if (response.getCode().equals(successCode[i])) {
				return response;
			}
		}

		if (ERRORCODE == response.getCode()) {
			throw new BirdRequestFailException(response.getDesc());
		} else if (RUNTIMEERRORCODE == response.getCode() || THIRDREQUESTERRORCODE == response.getCode()) {
			throw new RuntimeException(response.getDesc());
		} else {
			throw new BirdRequestException(response.getDesc());
		}
	}
}
